rm(list=ls())
library(tidyverse)

# Unzip files
zip_files <- list.files(here::here("data","raw","TSE"), pattern = "bem_candidato", full.names = T)
map(zip_files, unzip,
    exdir = here::here("data","raw","TSE","temp"),
    overwrite = F, 
    junkpaths = T)

# Prepare 2008 and 2012 data ---------------

# Read data
txt_files <- list.files(here::here("data","raw","TSE","temp"), pattern = ".txt", full.names = T)

self_reported_wealth <- map_df(txt_files, read_delim,
    delim = ";",
    locale = locale(encoding = "latin1"),
    col_names = c("data_geracao","hora_geracao","ano_eleicao",
                  "descricao_eleicao","sigla_uf","sq_candidato",
                  "cd_tipo_bem_candidato",
                  "ds_tipo_bem_candidato",
                  "detalhe_bem",
                  "valor_bem",
                  "data_ultima_atualizacao",
                  "hora_ultima_atualizacao"),
    col_types = "ccicccdccdcc"
    )

# Aggregate at candidate level
self_reported_wealth <- self_reported_wealth %>% 
  group_by(descricao_eleicao,ano_eleicao,sigla_uf,sq_candidato) %>% 
  summarise(self_reported_wealth = sum(valor_bem, na.rm = T)) %>% 
  mutate(descricao_eleicao = str_to_upper(stringi::stri_trans_general(descricao_eleicao, "latin-ascii"))) %>% 
  ungroup()

# save
write_rds(self_reported_wealth, here::here("data","processed","elections","self_reported_wealth.rds"))

# Delete temp folder with unzipped files
unlink(here::here("data","raw","TSE","temp"), recursive = T)
